package com.mdl.sponsorme.comunicacion;

import java.io.IOException;

import javax.servlet.http.*;

import com.mdl.sponsorme.controlador.*;
import com.mdl.sponsorme.modelo.Usuario;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

@SuppressWarnings("serial")
public class LoginUsuarioServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		
		UserService userService = UserServiceFactory.getUserService();
		ControladorWeb controladorWeb = new ControladorWeb();

        String thisURL = request.getRequestURI();
        if (request.getUserPrincipal() != null) {
        	Usuario usuario = controladorWeb.getUsuario(new Usuario(request.getUserPrincipal().getName()));
        	if(usuario!=null){
        		response.getWriter().println(usuario.toString());
        		response.getWriter().println("\t");
	            response.getWriter().println("<p>Hello, " +
	                                         request.getUserPrincipal().getName() +
	                                         "!  You can <a href=\"" +
	                                         userService.createLogoutURL(thisURL) +
	                                         "\">sign out</a>.</p>");
        	}
        } else {
            response.getWriter().println("<p>Please <a href=\"" +
                                         userService.createLoginURL(thisURL,"www.facebook.com") +
                                         "\">sign in</a>.</p>");
        }		
	}
}
